package cn.itcast.bassm.util;

import java.io.Writer;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import cn.itcast.bassm.message.ImageMessage;
import cn.itcast.bassm.message.MusicMessage;
import cn.itcast.bassm.message.NewsMessage;
import cn.itcast.bassm.message.TextMessage;
import cn.itcast.bassm.message.VideoMessage;
import cn.itcast.bassm.message.VoiceMessage;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;

public class MessageUtil {

  
    /** 
     * 请求消息类型：文本 
     */  
    public static final String REQ_MESSAGE_TYPE_TEXT = "text";  
  
    /** 
     * 请求消息类型：图片 
     */  
    public static final String REQ_MESSAGE_TYPE_IMAGE = "image";  
  
    /** 
     * 请求消息类型：音频 
     */  
    public static final String REQ_MESSAGE_TYPE_VOICE = "voice";
    
    /** 
     * 请求消息类型：音频 
     */  
    public static final String REQ_MESSAGE_TYPE_VIDEO = "video";
    
    /** 
     * 请求消息类型：地理位置 
     */  
    public static final String REQ_MESSAGE_TYPE_LOCATION = "location";  
    
    /** 
     * 请求消息类型：链接 
     */  
    public static final String REQ_MESSAGE_TYPE_LINK = "link";  
  
    /**
     * 事件
     */
    public static final String REQ_MESSAGE_TYPE_EVENT = "event";  
  
    
    /** 
     * 请求消息类型：文本 
     */  
    public static final String RESP_MESSAGE_TYPE_TEXT = "text";  
  
    /** 
     * 请求消息类型：图片 
     */  
    public static final String RESP_MESSAGE_TYPE_IMAGE = "image";  
  
    /** 
     * 请求消息类型：音频 
     */  
    public static final String RESP_MESSAGE_TYPE_VOICE = "voice";
    
    /** 
     * 请求消息类型：音频 
     */  
    public static final String RESP_MESSAGE_TYPE_VIDEO = "video";
    
    /** 
     * 请求消息类型：地理位置 
     */  
    public static final String RESP_MESSAGE_TYPE_MUSIC = "music";  
    
    /** 
     * 请求消息类型：链接 
     */  
    public static final String RESP_MESSAGE_TYPE_NEWS = "news";  
    
    /** 
     * 事件类型：subscribe(订阅) 
     */  
    public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";  
  
    /** 
     * 事件类型：unsubscribe(取消订阅) 
     */  
    public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";  
  
    /** 
     * 事件类型：CLICK(自定义菜单点击事件) 
     */  
    public static final String EVENT_TYPE_CLICK = "CLICK"; 
    
    //解析请求
    public static HashMap<String, String>parseXML(HttpServletRequest request) throws Exception{
    	
    	HashMap<String, String>map = new HashMap<String, String>();
    	
    	//通过IO获得Document
		SAXReader reader = new SAXReader();
		Document doc = reader.read(request.getInputStream());
		
		//得到根节点
		Element root=doc.getRootElement();
		
		recursiveParseXML(root,map);
    	
    	return map;
    }
    
    public static void recursiveParseXML(Element root,HashMap<String, String> map){
		//得到根节点的子节点列表
		List<Element>lementList=root.elements();
		//判断有没有子元素列表
		if(lementList.size()==0){
			map.put(root.getName(), root.getTextTrim());
			System.out.println(root.getName()+"=========================="+root.getTextTrim());
		}
		else {
			//遍历 
			for(Element e:lementList){
				recursiveParseXML(e,map);
			}
			
		}
    }
    
	private static XStream xstream = new XStream(new XppDriver() {
		public HierarchicalStreamWriter createWriter(Writer out) {
			return new PrettyPrintWriter(out) {
				// 对所有xml节点都增加CDATA标记
				boolean cdata = true;

				public void startNode(String name, Class clazz) {
					super.startNode(name, clazz);
				}

				protected void writeText(QuickWriter writer, String text) {
					if (cdata) {
						writer.write("<![CDATA[");
						writer.write(text);
						writer.write("]]>");
					} else {
						writer.write(text);
					}
				}
			};
		}
	});
	
	public static String messageToXML(TextMessage textMessage ){
		xstream.alias("xml", TextMessage.class);
		return xstream.toXML(textMessage);	
	}
	
	public static String messageToXML(ImageMessage imageMessage ){
		xstream.alias("xml", ImageMessage.class);
		return xstream.toXML(imageMessage);	
	}
	
	public static String messageToXML(MusicMessage musicMessage ){
		xstream.alias("xml", MusicMessage.class);
		return xstream.toXML(musicMessage);	
	}
	
	public static String messageToXML(NewsMessage newsMessage ){
		xstream.alias("xml", NewsMessage.class);
		xstream.alias("item", Article.class);
		return xstream.toXML(newsMessage);	
	}
	
	public static String messageToXML(VideoMessage videoMessage ){
		xstream.alias("xml", VideoMessage.class);
		return xstream.toXML(videoMessage);	
	}
	
	public static String messageToXML(VoiceMessage voiceMessage ){
		xstream.alias("xml", VoiceMessage.class);
		return xstream.toXML(voiceMessage);	
	}
	

}
